﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RastinArgham.CRM.Business;
using System.IO;
using Mani.Common.Security;

namespace RastinArgham.Support
{
    class Program
    {
        static void Main(string[] args)
        {
            CRMRepository repo = new CRMRepository();
            SecurityManager securityManager = new SecurityManager();
            aspnet_Users robot = securityManager.GetUserInfo("crm_robot");

            StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "\\modems-edit.csv");
            StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\result.log");
            
            int count = -1;
            int success = 0;

            while ( !sr.EndOfStream ) {
                count++;
                string[] modemInfo = sr.ReadLine().Split(',');
                
                if (count == 0)
                    continue;

                string serial = modemInfo[0];

                StockItem s = repo.GetStockItemBySerial(serial);
                if  ( s == null ) {
                    Console.WriteLine("Modem Not Found. Serial Number: " + serial);
                    sw.WriteLine(serial);
                    continue;
                }

                s.ISDN = modemInfo[2];
                s.Password = modemInfo[3];
                s.SubID = modemInfo[4];
                s.PaymentCode = modemInfo[5];
                s.LastUpdate = DateTime.Now;
                s.OperatorId = robot.UserId;

                repo.Save();
                success++;
            }

            sw.Flush();
            sw.Close();
            sw.Dispose();
            sr.Close();
            sr.Dispose();

            Console.WriteLine(success.ToString() + " from " + count.ToString() + " job was successfully executed!");
            Console.ReadLine();
        }
    }
}
